﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Printing;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Image = System.Windows.Controls.Image;
using Size = System.Windows.Size;

namespace Project
{
    /// <summary>
    /// Interaction logic for PrintPreviewDialog.xaml
    /// </summary>
    public partial class PrintPreviewDialogV2 : Window
    {
        private ObservableCollection<UserControl> _elements;
        private ObservableCollection<Image> _images; 

        public PrintPreviewDialogV2(ObservableCollection<UserControl> elements)
        {
            InitializeComponent();
            _elements = new ObservableCollection<UserControl>();
            _elements = elements;
            _images = new ObservableCollection<Image>();
            //_elements.Add(new PrintDirectAction());
            //_elements.Add(new PrintDirectionActionPart2());

            foreach (var userControl in _elements)
            {
                userControl.ApplyTemplate();
                userControl.Measure(new Size(793, 1122));
                userControl.Arrange(new Rect(0, 0, 793, 1122));
                userControl.UpdateLayout();
                _images.Add(new Image { Source = CaptureScreen(userControl), Width = 396, Height = 561 });
            }
            foreach (var image in _images)
            {
                ExampleCarouselControl.Children.Add(image);
            }
            if (ExampleCarouselControl.Children.Count == 2)
                ExampleCarouselControl.Children.Add(new Image { Width = 396, Height = 561 });
            ExampleCarouselControl.SetElementPositions();
            ExampleCarouselControl.ReInitialize();
            ExampleCarouselControl.UpdateLayout();
        }


        private BitmapSource CaptureScreen(Visual target)
        {
            if (target == null)
            {
                return null;
            }
            Rect bounds = VisualTreeHelper.GetDescendantBounds(target);
            RenderTargetBitmap rtb = new RenderTargetBitmap(793,
                                                            1122,
                                                            96,
                                                            96,
                                                            PixelFormats.Pbgra32);
            DrawingVisual dv = new DrawingVisual();
            using (DrawingContext ctx = dv.RenderOpen())
            {
                VisualBrush vb = new VisualBrush(target);
                ctx.DrawRectangle(vb, null, new Rect(new System.Windows.Point(), bounds.Size));
            }
            rtb.Render(dv);
            return rtb;
        }

        public void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            if ((bool)pd.ShowDialog().GetValueOrDefault())
            {
                foreach (var elem in _elements)
                {
                    pd.PrintVisual(elem, elem.Name);
                }
            }
        }

        private void ExampleCarouselControl_OnElementSelected(object sender)
        {
            //throw new NotImplementedException();
        }
    }
}
